from PyQt4.QtGui import QWidget,QTabWidget,QMainWindow,QAction,QKeySequence,QVBoxLayout,QToolBox
from PyQt4 import QtCore
from KUI.KSetting.ui_KSettingsWidget import Ui_KSettingsWidget
import pyqtgraph as pg
import pyqtgraph.parametertree.parameterTypes as pTypes
from pyqtgraph.parametertree import Parameter, ParameterTree, ParameterItem, registerParameterType
import cPickle as pickle
from KUtility.KConfig import KConfig

class KSettingsWidget(QWidget):
	
	SymbolChanged=QtCore.pyqtSignal(str) # triggerd by smart look up
	def __init__(self, parent=None):
		super(KSettingsWidget, self).__init__(parent)
		self.ui =  Ui_KSettingsWidget()
		self.ui.setupUi(self) 
		
		mlayout=QVBoxLayout(self.ui.frmPageContainer)
		mlayout.setContentsMargins(0,0,0,0) # set blank space between outside of layout and its parent widget		
		mlayout.setSpacing(0)
		self.ui.BtnSave.clicked.connect(self.saveSettings)
		self.ui.BtnCancel.clicked.connect(self.cancelSettings)		
		
		mtbox=QToolBox()
		mlayout.addWidget(mtbox)


		# create settings:
		#paraobjdict=KConfig.CreateSettings()
		
		# read settings 		
		paraobjdict=KConfig.GetSettings()
		self.paraobjdict=paraobjdict


		
		# create setting pages based on restored dict
		pageindex=0
		for each_page in sorted(paraobjdict):				
			mwdgt=ParameterTree()
			mwdgt.setParameters(paraobjdict[each_page], showTop=False)		
			mtbox.insertItem(pageindex,mwdgt,each_page)
			pageindex+=1

	def saveSettings(self):
		if hasattr(self,'paraobjdict'):
			KConfig.SaveSettings(self.paraobjdict)
		
	def cancelSettings(self):
		pass

